<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('基金名称')" prop="fundName">
            <el-input
              v-model="form.fundName"
              disabled
              :placeholder="$t('基金名称')"
              show-word-limit
              maxlength="32"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('分配规则')" prop="allocationRules">
            <el-input
              v-model="form.allocationRules"
              :placeholder="$t('分配规则')"
              show-word-limit
              maxlength="50"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('出资人类型')">
            <el-select v-model="form.investorType" multiple value="" :placeholder="$t('出资人类型')">
              <el-option
                v-for="dict in investorTypeOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('合伙人类型')" prop="partnerType">
            <el-select v-model="form.partnerType" value="" :placeholder="$t('合伙人类型')" @change="partnerTypeChange">
              <el-option
                v-for="dict in partnerTypeOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col v-if="form.partnerType != 'GP'" :span="12">
          <el-form-item :label="$t('分配类型')" prop="allocationType">
            <el-select v-model="form.allocationType" value="" :placeholder="$t('分配类型')">
              <el-option
                v-for="dict in allocationTypeOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col v-if="form.partnerType != 'GP'" :span="24">
          <div class="area-header">
            <span class="area-header-title">{{ $t('Hurdle规则') }}</span>
          </div>
        </el-col>
        <el-col v-if="form.partnerType != 'GP'" :span="12">
          <el-form-item :label="$t('有无Hurdle')" prop="hurdle">
            <el-select v-model="form.hurdle" value="" :placeholder="$t('有无Hurdle')">
              <el-option
                v-for="dict in hurdleTypeOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col v-if="form.hurdle === 'yes'" :span="12">
          <el-form-item :label="$t('Hurdle计算方式')" prop="hurdleCalMethod">
            <el-select v-model="form.hurdleCalMethod" value="" :placeholder="$t('Hurdle计算方式')">
              <el-option
                v-for="dict in hurdleCalMethodOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col v-if="form.hurdle === 'yes'" :span="12">
          <el-form-item :label="$t('Hurdle比例(%)')" prop="hurdleRatio">
            <Ratio v-model="form.hurdleRatio" :placeholder="$t('hurdle比例(%)')" />
          </el-form-item>
        </el-col>
        <el-col v-if="form.hurdle === 'yes'" :span="12">
          <el-form-item :label="$t('每年计息天数')" prop="yearInterestDay">
            <Money v-model="form.yearInterestDay" :placeholder="$t('每年计息天数')" />
          </el-form-item>
        </el-col>
        <el-col v-if="form.partnerType != 'GP'" :span="24">
          <div class="area-header">
            <span class="area-header-title">{{ $t('Carry规则') }}</span>
          </div>
        </el-col>
        <el-col v-if="form.partnerType != 'GP'" :span="12">
          <el-form-item :label="$t('Carry比例LP(%)')" prop="carryRatioLp">
            <Ratio v-model="form.carryRatioLp" :placeholder="$t('Carry比例LP(%)')" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="fund:allocationRules" />
    </div>
  </div>
</template>

<script>
import {
  getFundAllocationRules,
  saveFundAllocationRules
} from '@/api/fund/allocationRules/fundAllocationRules'
import { getFundName } from '@/api/fund/fundBase'

export default {
  data() {
    return {
      // 出资人类型
      investorTypeOptions: [],
      partnerTypeOptions: [],
      // 分配类型
      allocationTypeOptions: [],
      // 有无hurdle
      hurdleTypeOptions: [],
      hurdleCalMethodOptions: [],
      // 表单参数
      form: {},
      // 遮罩层
      loading: false,
      // 表单校验
      rules: {
        allocationRules: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        investorType: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        partnerType: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        allocationType: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        hurdle: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        hurdleCalMethod: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        hurdleRatio: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        yearInterestDay: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        carryRatioLp: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        carryRatioGp: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
      }
    }
  },
  created() {
    // 出资类型
    this.getDicts('fund_investor_type').then(response => {
      response.data.forEach(item => {
        this.investorTypeOptions.push(item)
      })
    })
    this.getDicts('partner_type').then(response => {
      response.data.forEach(item => {
        this.partnerTypeOptions.push(item)
      })
    })
    this.getDicts('allocation_type').then(response => {
      this.allocationTypeOptions = response.data
    })
    this.getDicts('hurdle_type').then(response => {
      this.hurdleTypeOptions = response.data
    })
    this.getDicts('hurdle_cal_method').then(response => {
      this.hurdleCalMethodOptions = response.data
    })
  },
  methods: {
    // 初始化数据
    init(data) {
      this.loading = true
      if (data != null && data.id !== undefined) {
        getFundAllocationRules(data.id).then(response => {
          this.form = response.data
          this.loading = false
        })
      } else {
        this.reset()
        getFundName(this.$route.query.id).then(response => {
          this.$set(this.form, 'fundName', response)
          this.$set(this.form, 'fundId', this.$route.query.id)
          this.loading = false
        })
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        fundId: this.$route.query.id,
        fundName: undefined,
        allocationRules: undefined,
        investorType: undefined,
        allocationType: undefined,
        hurdleCalMethod: undefined,
        hurdle: undefined,
        hurdleRatio: undefined,
        yearInterestDay: undefined,
        carryRatioLp: undefined,
        carryRatioGp: undefined,
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.formOpen !== undefined) {
        this.$parent.$parent.formOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          saveFundAllocationRules(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess(this.$t('保存成功'))
              this.closeForm()
            } else {
              this.msgError(response.msg)
            }
            this.loading = false
          })
        } else {
          this.locationError(document)
        }
      })
    },
    partnerTypeChange() {
      if (this.form.partnerType === 'GP') {
        this.form.allocationType = 'byfund'
        this.form.hurdle = 'no'
        this.form.carryRatioLp = 100
        this.form.carryRatioGp = 0
      }
    }
  }

}
</script>
